#import "CabDriverVC.h"
#import "CabDriverMapVC.h"

@implementation CabDriverVC

@synthesize passenger;

- (void)viewDidLoad 
{
	_timer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(reload) userInfo:nil repeats:YES];
	[_timer fire];
	
	[super viewDidLoad];
}

- (void)reload
{
	passenger = [Passenger PassengerWithId:[passenger ID]];
	[_tableView reloadData];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
	return [[passenger cabid] isEqualToString:@"null"] ? 1 : 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
	if(section==0)
		return 2;
	else 
		return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
	static NSString *CellIdentifier = @"Cell";
	
	UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
	if (cell == nil) 
	{
		cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
	}
	
	NSString* cabid = [[passenger cabid] isEqualToString:@"null"] ? @"None" : [passenger cabid];
	NSString* cabstatus = [[passenger status] isEqualToString:@"null"] ? @"None" : [passenger status];
	
	if(indexPath.section)
	{
		cell.textLabel.text = @"See cab on map";
		cell.selectionStyle = UITableViewCellSelectionStyleBlue;
		return cell;
	}
	cell.selectionStyle = UITableViewCellSelectionStyleNone;

	switch(indexPath.row)
	{
		case 0:
			cell.textLabel.text = @"Cab ID";
			cell.detailTextLabel.text = cabid;
			break;
		case 1:
			cell.textLabel.text = @"Cab Status";
			cell.detailTextLabel.text = cabstatus;
			break;
		default:
			break;
	}
	return cell;
}



- (void)dealloc {
    [super dealloc];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 1)
		[self mapButtonPressed:tableView];
	[tableView deselectRowAtIndexPath:indexPath animated:YES];
}



- (IBAction)mapButtonPressed:(id)sender
{
	NSString* cabid = [passenger cabid];
	if([cabid isEqualToString:@"null"])
	{
		NSLog(@"Cab is null");
		return;
	}
	CabDriverMapVC* mapVC = [[CabDriverMapVC alloc] init];
	mapVC.cabDriver = [CabDriver CabDriverWithID:[[passenger cabid] integerValue]];
	[self.navigationController pushViewController:mapVC animated:YES];
}

@end
